package com.jiang.myfm;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioGroup;

import com.jiang.myfm.activity.AnchorHotActivity;
import com.jiang.myfm.activity.HotActivity;
import com.jiang.myfm.fragment.Downfragment;
import com.jiang.myfm.fragment.Findfragment;
import com.jiang.myfm.fragment.Listenfragment;
import com.jiang.myfm.fragment.Myfragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fragment> list;
    private RadioGroup rg;
    private int lock = 0;
    private Listenfragment mFragment2;
    public static int MAX_HEIGHT_TITLE;
    private Findfragment mFragment1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MAX_HEIGHT_TITLE = (int) (getResources().getDisplayMetrics().widthPixels / 7.7);
        rg = (RadioGroup) findViewById(R.id.rg);
        list = new ArrayList<>();
        mFragment1 = new Findfragment();
        mFragment2 = new Listenfragment();
        Downfragment fragment3 = new Downfragment();
        Myfragment fragment4 = new Myfragment();
        list.add(mFragment1);
        list.add(mFragment2);
        list.add(fragment3);
        list.add(fragment4);
        getSupportFragmentManager().beginTransaction().add(R.id.fl, mFragment1).commit();

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.tab_bt1:
                        changefragment(0);
                        break;
                    case R.id.tab_bt2:
                        changefragment(1);
                        break;
                    case R.id.tab_bt4:
                        changefragment(2);
                        break;
                    case R.id.tab_bt5:
                        changefragment(3);
                        break;
                }
            }
        });
    }

    private void changefragment(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment target = list.get(i);
        Fragment before = list.get(lock);
        if (target.isAdded()) {
            transaction.show(target).hide(before).commit();
        } else {
            transaction.add(R.id.fl, target).hide(before).commit();
        }
        lock = i;
    }

    public void btnOnclick(View view) {
        Intent intent = new Intent(MainActivity.this, HotActivity.class);
        startActivity(intent);
    }

    public void btnSub(View view) {
        mFragment2.getSetFirstListen().onSetFirstListen(0);
    }

    public void btnText(View view) {
        Intent intent=new Intent(MainActivity.this, AnchorHotActivity.class);
        startActivity(intent);
    }

    public void btnMore(View view) {
        mFragment1.getSetFirstListen().onSetFirstListen(1);
    }
}

